#!/bin/sh
#
# cluster_operator.sh
#

if [ $# -lt 4 ];then
  echo "Usage: cluster_operator.sh controllerip username configFile [add | remove | start | stop | monitor | addclient | removeclient] nodeip"
  exit 1;
fi

controllerip=$1
username=$2
configfile=$3
operatortype=$4

if [ $operatortype = "start" -o $operatortype = "stop" ];then
	
	su $username -c " ssh $controllerip \"source .bashrc; cd pgxc_ctl; pgxc_ctl -c $configfile $operatortype all\" "
fi

if [ $operatortype = "add" -o $operatortype = "remove" ];then
	nodeip=$5
	su $username -c " ssh $controllerip \"source .bashrc; cd pgxc_ctl; ./clusterAddRemoveNode.sh $configfile $controllerip $operatortype $nodeip\" "
fi

if [ $operatortype = "monitor" ];then
	
	su $username -c " ssh $controllerip \"source .bashrc; cd pgxc_ctl; pgxc_ctl -c $configfile $operatortype all\" "
fi

if [ $operatortype = "addclient" -o $operatortype = "removeclient" ];then
	if [ $operatortype = "addclient" ];then
		operatortype="add"
	else
		operatortype="remove"
	fi
	nodeip=$5
	su $username -c " ssh $controllerip \"source .bashrc; cd pgxc_ctl; ./refreshPgHba.sh $configfile $operatortype client $nodeip\" "
fi

if [ $? -ne 0 ]; then
	exit 100
fi

exit 0
